package org.iku.crawl.handler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.regex.MatchResult;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.http.HttpStatus;
import org.heydr.annotation.HandleInfo;
import org.heydr.common.TaskEngine;
import org.heydr.common.http.StringUtils;
import org.heydr.entity.Page;
import org.heydr.entity.Url;
import org.heydr.handler.HeyDrHandler;
import org.heydr.handler.impl.SubHandler;
import org.heydr.impl.SubCrawler;
import org.iku.common.RegexTool;
import org.iku.crawl.dao.ResourceDAO;
import org.iku.entity.Item;
import org.iku.entity.ItemUrl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@HandleInfo(group = "javimdb", step = 1)
public class JAVIMDBHandler extends SubHandler {
	private static Logger logger = LoggerFactory.getLogger(JAVIMDBHandler.class);
	
	@Autowired
	protected JAVIMDBInfoHandler infoHandler;
	
	@Override
	public Object handle(Url url, Object object) {
		Page page = (Page)object;
		if (null != page && HttpStatus.SC_OK == page.getHttpStatus()) {
			// 抓取成功
			snatchSuccess(url, page);
			//notify task engine that the current url has been done.
//			TaskEngine.finishTask(url);
		} else {
			// 抓取失敗
			logger.warn(url.getUrl()+ " fail!!");
			snatchFail(url);
		}
		return null;
	}

	public void snatchSuccess(Url url, Page page) {
		int newItemCount = 0;
		try {
			String content = page.getContent();
//			System.out.println(content);

			Map<String, ItemUrl> tempMap = new HashMap<String, ItemUrl>();

			List<MatchResult> seedsCn = RegexTool.multiExtractToMatchResult(content, "</a><div.*?\"(http://www\\.javimdb\\.com/cn/movie/.*?)\" target=\"_blank\">(.*?)</a><br>");
			for (MatchResult match : seedsCn) {
				ItemUrl itemUrl = new ItemUrl();
				Url tempUrl = (Url) BeanUtils.cloneBean(url);
				tempUrl.setUrl(match.group(1));
				itemUrl.setUrl(tempUrl);
				Item item = new Item();
				item.setTitleCn(match.group(2));
				itemUrl.setItem(item);
				tempMap.put(StringUtils.md5(tempUrl.getUrl()), itemUrl);
			}

			/* 英文补全 */
			Url urlEn = (Url) BeanUtils.cloneBean(url);
			urlEn.setUrl(url.getUrl().replace("/cn/", "/en/"));
			Page enPage = SubCrawler.snatch(urlEn);
			String contentEn = enPage.getContent();
			List<MatchResult> seedsEn = RegexTool.multiExtractToMatchResult(contentEn, "</a><div.*?\"(http://www\\.javimdb\\.com/en/movie/.*?)\" target=\"_blank\">(.*?)</a><br>");

			if (null != seedsEn) {
				for (MatchResult match : seedsEn) {
					String subUrlEn = match.group(1);
					subUrlEn = subUrlEn.replace("/en/", "/cn/");
					String md5 = StringUtils.md5(subUrlEn);
					if(TaskEngine.alreadySnatched(md5)){
						/* 本列表頁已出現抓去過的作品，停止翻頁 */
						continue;
					}else{
						newItemCount ++;
					}
					ItemUrl itemUrl = tempMap.get(md5);
					if (null != itemUrl) {
						itemUrl.getItem().setTitleEn(match.group(2));
						tempMap.put(md5, itemUrl);
						try {
							//抓取作品詳情頁
							infoHandler.run(itemUrl.getUrl(), itemUrl.getItem());
						} catch (Exception e) {
							
						}
					}
				}
			}else{
				return;
			}
			
			if(newItemCount>0){
				String next = RegexTool.singleExtract(content, "<li><a href=\"(.*?)\">下一页</a>", 1);
				if(null != next){
					next = url.getSite() + next;
					// 將下一頁添加至任務隊列
					url.setUrl(next);
					System.out.println(url.getUrl()+" in!!");
					TaskEngine.newTask(url);
				}
			}
		} catch (Exception e) {
		}
	}

	/**
	 * <strong>Description</strong>:抓取失敗，重新添加進任務隊列
	 * 
	 * @param url
	 * @return void
	 * @author chok 2014-3-1
	 */
	public void snatchFail(Url url) {
		if (url.getFailTimes() < 3) {
			url.setFailTimes(url.getFailTimes()+1);
			TaskEngine.newTask(url);
		}
	}

}
